<template>
  <form method="post" id="pay-cmbc-form">

  </form>
</template>
<script type="text/javascript">
  import utils from '@/util/util';
  import Api from '@/util/api';
  export default {
    data(){
      return {
        orderId:0,
        platform:"",
        payType:"",
      }
    },
    mounted(){
      if(!sessionStorage.getItem("smbc_pay")){
        this.loading("请稍等...");
        var {orderId,platform,payType} = this.$route.query;
        this.orderId = orderId;
        this.platform = platform;
        this.payType = payType;
        if(orderId && platform && payType){
          this.getpayInfo();
        }else{
          this.$vux.loading.hide();
          this.toast("参数错误！");
          setTimeout(()=>{
            this.$router.replace("/find");
          },2000);
        }
      }else{
        this.$warn("goBack");
        this.goBack();
      }
    },
    methods:{
        goBack(){
          if(utils.goBack()){
            sessionStorage.removeItem("smbc_pay");
            this.$router.go(-1);
          }
        },
        getpayInfo(){
          var token = utils.getUserToken();
          if(token){
            var orderId = this.orderId;
            var platform = this.platform;
            var payType = this.payType;
            this.post(Api.newOrderPayment,{fullPay:true,orderId,platform,payType},{token})
            .then((d)=>{
              this.$vux.loading.hide();
              console.log(d);
              if(d.code == 200){
                  var dom = document.getElementById("pay-cmbc-form");
                  try{
                    var ojson = JSON.parse(d.data.param);
                    var fromUrl = d.data.url;
                    dom.action=fromUrl;
                    var inputs = [];
                    for(var ikey in ojson){
                      var inputXML = `<input type='hidden' name='${ikey}' value='${ojson[ikey]}' />`
                      inputs.push(inputXML);
                    }
                    dom.innerHTML = inputs.join(" ");
                    sessionStorage.setItem("smbc_pay","1");
                    this.toast("跳转中,请稍等...");
                    dom.submit();
                  }catch(e){
                    console.log(e);
                    this.toast("获取后台支付信息错误！");
                  }

              }else{
                this.toast(d.message);
              }
            })
          }else{
            this.toast("获取用户信息失败,请重试~");
          }
        }
    }
  }
</script>
<style lang='less' rel='stylesheet/less'>
  @import '../styles/theme.less';

</style>
